package com.hst.web.base.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.hst.core.ServiceContext;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ISQLDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.json.JsonUtil;
import com.hst.web.auth.po.User;
import com.hst.web.base.entity.TCompanyEmployee;
import com.hst.web.base.entity.TVehicleInfo;
import com.hst.web.base.service.UserRoleService;
import com.hst.web.base.service.VehicleService;
import com.hst.web.base.ui.VehicleReview;
import com.hst.web.sys.entity.TUser;
import com.hst.web.sys.entity.TUserRole;

@Controller
@RequestMapping("/vehicle")
public class VehicleController {

	Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	IORDao dao;

	@Autowired
	ISQLDao sql;

	@Autowired
	VehicleService service;

	@SuppressWarnings({ "unchecked" })
	private Map<String, Object> parseQuerys(String data) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> querys = JsonUtil.read(data, Map.class);
		return querys;
	}

	@RequestMapping("/dict")
	public void vehicleDict(Model m) {
		Map<String, Object> params = new HashMap<String, Object>();
		UserRoleService.addCompanyEmpParam(params);
		m.addAttribute("vehicle", sql.query("vehicle.dict", params));
	}

	@RequestMapping("/employee")
	public void employee(String employeeCode, Model m) {
		m.addAttribute("data", dao.get(TCompanyEmployee.class, employeeCode));
	}

	@RequestMapping("/next-first")
	public void getNext(Model m) throws Exception {
		TUser u = dao.get(TUser.class, ((User) ServiceContext.getInstance().getUser()).getId());
		List<TUserRole> l = dao.list(TUserRole.class, new ORQuery(Op.eq, "userid", u.getId()));
		if (l.size() != 1) {
			logger.error("");
			throw new Exception();
		}

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roleid", l.get(0).getRoleid());

		@SuppressWarnings("unchecked")
		List<Map<String, Object>> ln = (List<Map<String, Object>>) sql.query("vehicle.list-next", params);
		if (ln.size() > 0) {
			ln.get(0).get("role");
		}

		// m.addAttribute("next", ln.get(0));
	}

	@RequestMapping("/vehicleDetail")
	public void vehicleDetail(@RequestBody String data, Model m)
			throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> params = parseQuerys(data);
		String vehicleCode = (String) params.get("vehicleCode");
		TVehicleInfo vehicle = dao.get(TVehicleInfo.class, vehicleCode);
		m.addAttribute("vehicle", JsonUtil.toString(vehicle));
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/review-approve")
	public void reviewApprove(@RequestBody String data, Model m)
			throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> map = JsonUtil.read(data, Map.class);
		String s1 = (String) map.get("list");
		List<VehicleReview> list = JsonUtil.readList(s1, VehicleReview.class);
		String remark = (String) map.get("remark");
		service.reviewApprove(list, remark);
		m.addAttribute("data", "0");
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/review-reject")
	public void reviewReject(@RequestBody String data, Model m)
			throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> map = JsonUtil.read(data, Map.class);
		String s1 = (String) map.get("list");
		List<VehicleReview> list = JsonUtil.readList(s1, VehicleReview.class);
		String remark = (String) map.get("remark");
		service.reviewReject(list, remark);
		m.addAttribute("data", "0");
	}
}
